package user

import (
	"chat/common/response"
	"chat/common/validate"
	"chat/restful/internal/logic/user"
	"chat/restful/internal/svc"
	"chat/restful/internal/types"
	"fmt"
	"github.com/zeromicro/go-zero/rest/httpx"
	"google.golang.org/grpc/status"
	"net/http"
	"strings"
)

func AddFlowedHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.UserAppleRequest
		httpx.Parse(r, &req)
		validateErr := validate.Validate(req)
		if validateErr != nil {
			fmt.Println(validateErr)
			response.Response(w, nil, response.CodeParamsError)
			return
		}
		l := user.NewAddFlowedLogic(r.Context(), svcCtx)
		err := l.AddFlowed(&req)
		if err != nil {
			fromErr, _ := status.FromError(err)
			if fromErr.Code() == 899 {
				//申请的群聊达到最大限制
				response.Response(w, nil, response.CodeGroupMax)
				return
			}
			if strings.Contains(err.Error(), "repeat addition") {
				response.Response(w, nil, response.CodeRepeatAddition)
				return
			}
			if strings.Contains(err.Error(), "group restful overload") {
				response.Response(w, nil, response.CodeGroupChatOverload)
				return
			}
			response.Response(w, nil, response.CodeServerBusy)
		} else {
			response.Response(w, nil, response.CodeSuccess)
		}
	}
}
